﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HomeWork7_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("必应");
            comboBox1.Items.Add("百度");
            comboBox1.SelectedIndex = 0;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            string server = this.comboBox1.Text;
            string url = "";
            if (server == "必应")
                url = @"https://cn.bing.com/";
            else
                url= @"http://www.baidu.com";
            string opt = this.textBox1.Text;
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            client.DownloadStringCompleted += DownloadCompleted;
            client.DownloadStringAsync(new Uri(url));
            label3.Text = "爬取网页中...";

        }
        private void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            label3.Text = "爬取成功";
            if (!e.Cancelled && e.Error == null)
            {
                richTextBox1.Text = (String)e.Result;
            }
            else
            {
                richTextBox1.Text = "" + e.Error;
            }
        }
        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }
        private void DownloadCompletedForNumber(object sender, DownloadStringCompletedEventArgs e)
        {
            label3.Text = "爬取成功";
            if (!e.Cancelled && e.Error == null)
            {
                string s= (String)e.Result;
                string regex1 = @"(1\d{2,3}|0\d{2,}-)\d{7,}";
                string text = "";
                MatchCollection result = Regex.Matches(s, regex1);
                foreach (Match m in result)
                {
                    text += m + "\n";
                }
                richTextBox1.Text = text;
            }
            else
            {
                richTextBox1.Text = "" + e.Error;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            string server = this.comboBox1.Text;
            string url = "http://www.moe.gov.cn/jyb_xwfb/gzdt_gzdt/s5987/202106/t20210629_540937.html";
            if (keyword.Text != DEFAULT_TEXT && keyword.Text != "")
                url = keyword.Text;
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            client.DownloadStringCompleted += DownloadCompletedForNumber;
            label3.Text = "爬取中高校号码中...";
            try
            {
                client.DownloadStringAsync(new Uri(url));

            }catch (Exception)
            {

                MessageBox.Show("请输入正确的网址", "提示");
                label3.Text = "请重新爬取";
            }
            
        }
    }
}
